package doru;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;



/**
 * Takes in a domain and checks if it is listed as malicious by any of the listed sites. a method is used to pull site down and then search for string only
 * present for malicious domains. all sites being checked have var String site andString siteIsMalCheck.
 */
public class CheckUrl {

	//in case you want to know what the current url is.
	public String url;

	/**
	 * some sites respond very slowly. To improve speed and memory usage all of the static checks are declared here. if speed is an issue, create a new checkurl instance for every domain you are checking.
	 */

	//Google
	static String google = "http://google.com/safebrowsing/diagnostic?site=http://";
	static String googleIsMalCheck = "Site is listed as suspicious";
	static String googleIsUnvCheck = "not visited this site within the past 90 days";
	
	//Norton
	static String norton = "http://safeweb.norton.com/report/show?url=";
	static String nortonIsMalCheck = "/images/icons/large_sphere/large-red.png";
	static String nortonIsGoodCheck = "/images/icons/large_sphere/large-green.png";

	//Mcafee
	static String mcafee = "http://www.mcafee.com/threat-intelligence/domain/default.aspx?domain=";
	static String mcafeeIsMalCheck = "/img/banners/threat/risk-meters/rm-webrep-high.png";
	static String mcafeeIsMedMalCheck = "/img/banners/threat/risk-meters/rm-webrep-med.png";
	static String mcafeeIsLowMalCheck = "/img/banners/threat/risk-meters/rm-webrep-low.png";
	static String mcafeeIsUnvCheck = "/img/banners/threat/risk-meters/rm-webrep-unv.png";

	//MyWot
	static String mywot = "http://www.mywot.com/en/scorecard/";
	static String mywotIsMalCheck = "This site has a poor reputation.";
	static String mywotIsVirusCheck = "Malicious content, viruses";
	static String mywotIsExploitCheck = "Browser exploit";


	//created to help streamline code in main. 
	public CheckUrl(){

	}

	//takes in URL or IP
	public CheckUrl(String passedUrl) {

		url = passedUrl;
	}

	//setter if blank constructor is used. because of latency issues I would not use this for a whole bunch of domains being searched at once.
	public void setUrl(String passedurl) {
		url = passedurl;
	}

	//basic check to mywot, this will need to be fleshed out when API is included
	public String mywotResult() {
		String fullSite = returnFullSite(url, mywot);

		if (fullSite.contains("whoops")){
			return "Site Error";
		}

		if (fullSite.contains(mywotIsMalCheck) || fullSite.contains(mywotIsVirusCheck) || fullSite.contains(mywotIsExploitCheck)){
			return "Malicious";
		}else{
			return "Clean";
		}
	}
	//Medium and High severity are considered malicious and low is considered Clean. Unverified is just that.
	public String mcafeeResult() {
		String fullSite = returnFullSite(url, mcafee);

		if (fullSite.contains("whoops")){
			return "Site Error";
		}

		if (fullSite.contains(mcafeeIsMalCheck) || fullSite.contains(mcafeeIsMedMalCheck)){
			return "Malicious";
		}
		if (fullSite.contains(mcafeeIsUnvCheck)){
			return "Unverified";
		}
		if (fullSite.contains(mcafeeIsLowMalCheck)){
			return "Clean";
		}

		return "Whoops";
	}


	//Google check is simple and easy.
	public String googleResult() {

		String fullSite = returnFullSite(url, google);

		if (fullSite.contains("whoops")){
			return "Site Error";
		}

		if (fullSite.contains(googleIsMalCheck)){
			return "Malicious";
		}
		if(fullSite.contains(googleIsUnvCheck)){
			return "Unverified";
		}
		return "Clean";
	}
	

	//Norton is very slow but easy to check.
	public String nortonResult() {
		String fullSite = returnFullSite(url, norton);

		if (fullSite.contains("whoops")){
			return "Site Error";
		}

		if(fullSite.contains(nortonIsMalCheck)){
			return "Malicious";
		}
		if(fullSite.contains(nortonIsGoodCheck)){
			return "Clean";
		}
		return "Unverified";
	}



	/**
	 * Take url and append to site. Use a stringbuilder to pull the site down and 
	 * put in completeSite, pass this back. If something goes wrong tell the requester whoops.
	 */
	private static String returnFullSite(String urlToCheck, String site){


		String completeSite = null;


		try {

			//magic happens here
			URL url = new URL(site + urlToCheck);
			URLConnection urlc = url.openConnection();

			BufferedInputStream buffer = new BufferedInputStream(urlc.getInputStream());

			StringBuilder builder = new StringBuilder();
			int byteRead;
			while ((byteRead = buffer.read()) != -1)
				builder.append((char) byteRead);

			buffer.close();
			completeSite = builder.toString();

			System.out.println(url); //used for troubleshooting flow


		} catch (MalformedURLException ex) {
			ex.printStackTrace();
			return "whoops"; //used to populate cell in table in case of error

		} catch (IOException ex) {
			ex.printStackTrace(); 
			return "whoops"; //used to populate cell in table in case of error
		}

		return completeSite;
	}


}




